package com.spring.springboot.util;

import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.UUID;

/**
 * @author 15625
 * @date 2021-03-16 16:32
 * @description: 图片处理工具
 */
@Component
public class ImageUtils {

    /**
     * 水印透明度
     */
    private static float alpha = 1f;
    /**
     * 水印横向位置
     */
    private static int positionWidth = 150;
    /**
     * 水印纵向位置
     */
    private static int positionHeight = 300;
    /**
     * 水印文字字体
     */
    private static Font font = new Font("宋体", Font.BOLD, 72);
    /**
     * 水印文字颜色
     */
    private static Color color = Color.red;

    /**
     * 给图片添加水印图片、可设置水印图片旋转角度
     *
     * @param iconPath   水印图片路径
     * @param srcImgPath 源图片路径
     * @param targerPath 目标图片路径
     * @param degree     水印图片旋转角度
     */
    public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree, Integer w, Integer h) {

        String uuid = UUID.randomUUID().toString();

        OutputStream os = null;
        try {

            Image srcImg = ImageIO.read(new File(srcImgPath));
            //图片长
            int srcImageHeight = srcImg.getHeight(null);
            //图片宽
            int srcImageWidth = srcImg.getWidth(null);
            System.out.println("图片尺寸：图片高：" + srcImageHeight + "图片宽：" + srcImageWidth);

            Image iconImg = ImageIO.read(new File(iconPath));
            //图片长
            int iconImageHeight = iconImg.getHeight(null);
            //图片宽
            int iconImageWidth = iconImg.getWidth(null);
            System.out.println("Logo图片尺寸：图片高：" + iconImageHeight + "图片宽：" + iconImageWidth);

            int sw = (srcImageWidth - iconImageWidth) / 2;
            int sh = (srcImageHeight - iconImageHeight) / 2;

            if (w == null && h == null) {
                w = sw;
                h = sh;
            }
            System.out.println("二维码位置：宽" + w + "    " + "高" + h);

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 1、得到画笔对象
            Graphics2D g = buffImg.createGraphics();

            // 2、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
            // 3、设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
            }

            // 4、水印图片的路径 水印图片一般为gif或者png的，这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(iconPath);
//            ImageIcon imgIcon = new ImageIcon(dest);

            // 5、得到Image对象。
            Image img = imgIcon.getImage();

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

            // 6、水印图片的位置
            g.drawImage(img, w, h, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            // 7、释放资源
            g.dispose();

            // 8、生成图片
            os = new FileOutputStream(targerPath);
            ImageIO.write(buffImg, "JPG", os);

            System.out.println("图片完成添加水印图片");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 按照缩放比例缩放图片
     * src :    图片的原路径
     * dest :   缩放后图片的存储路径
     * w    :   缩放图片的目标宽度
     * h    ：  缩放图片的目标高度
     */
    public static void zoomImage(String src, String dest, int w, int h) throws Exception {
        double wr = 0, hr = 0;
        File srcFile = new File(src);
        File destFile = new File(dest);

        //读取图片
        BufferedImage bufImg = ImageIO.read(srcFile);
        //设置缩放目标图片模板
        Image Itemp = bufImg.getScaledInstance(w, h, Image.SCALE_SMOOTH);

        //获取缩放比例
        wr = w * 1.0 / bufImg.getWidth();
        hr = h * 1.0 / bufImg.getHeight();

        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
        Itemp = ato.filter(bufImg, null);
        try {
            //写入缩减后的图片
            ImageIO.write((BufferedImage) Itemp, dest.substring(dest.lastIndexOf(".") + 1), destFile);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
